package com.attendance.file.controller;

import com.attendance.java.StringUtils;
import com.attendance.utils.aliyun.oss.AliyunOss;
import com.attendance.utils.aliyun.oss.OssFile;
import com.attendance.utils.aliyun.oss.OssFileResult;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 *
 * @author ruoyi
 */
@Controller
public class CommonController {

    @Autowired
    AliyunOss aliyunOss;

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ResponseBody
    public Result uploadFile(MultipartFile file) throws Exception {
        try {
            OssFile ossFile = new OssFile();
            ossFile.setUrl("notice/");
            String s = DigestUtils.sha1Hex(file.getBytes());
            ossFile.setFileName(s + "." + StringUtils.getExtend(file.getOriginalFilename(), "txt"));
            ossFile.setFileData(file.getBytes());
            OssFileResult result = aliyunOss.uploadByteArray(ossFile);
            return new Result(ResultCode.SUCCESS, result);
        } catch (Exception e) {
            return new Result(ResultCode.FAIL, e.getMessage());
        }
    }
}
